'''
Created on Jan 9, 2010

@author: iGee
'''
import math
class Matrix:
    @staticmethod
    def getScaleMatrix(scaleX, scaleY):
        transformMatrix = [[0]*3 for i in range(3)]
        
        transformMatrix[0][0] = scaleX
        transformMatrix[1][1] = scaleY
        transformMatrix[2][2] = 1
        
        return transformMatrix
     
    @staticmethod
    def getShearMatrixTest(shearX,shearY):
        transformMatrix = [[0]*3 for i in range(3)]

        transformMatrix[0][0] = 0
        transformMatrix[0][1] = shearX
        transformMatrix[1][1] = 0
        transformMatrix[1][0] = shearY
        transformMatrix[2][2] = 1
        return transformMatrix
    
    @staticmethod
    def getShearMatrix(shearX,shearY):
        transformMatrix = [[0]*3 for i in range(3)]
        
        transformMatrix[0][0] = 1
        transformMatrix[0][1] = shearX
        transformMatrix[1][1] = 1
        transformMatrix[1][0] = shearY
        transformMatrix[2][2] = 1
        return transformMatrix  
    
    @staticmethod
    def getTranslateMatrix(movex, movey):
        transformMatrix = [[0.0]*3 for i in range(3)]
        transformMatrix[0][0] = 1.0
        transformMatrix[0][2] = movex
        transformMatrix[1][1] = 1.0
        transformMatrix[1][2] = movey
        transformMatrix[2][2] = 1.0
        return transformMatrix
    
    @staticmethod
    def getRotateMatrix(angle):
        transformMatrix = [[0]*3 for i in range(3)]
        #convert angle to radiant
        angle = angle*math.pi/180
        
        transformMatrix[0][0] = math.cos(angle)
        transformMatrix[0][1] = -math.sin(angle)
        transformMatrix[1][0] = math.sin(angle)
        transformMatrix[1][1] = math.cos(angle)
        transformMatrix[2][2] = 1

        return transformMatrix
    
    @staticmethod
    def MultiplyMatrixTQ(A=[], B=[]):
        #so cot cua A phai bang so Hang cua B
        '''
        if len(A[0]) != len(B):
            #print "matrix ", A, " \n multiply by matrix \n ", B, "\n is invalid" 
            return
        '''    
        result = [[0]*len(B[0]) for i in range(len(A))]

        for i in range(len(B[0])): #cot doc, ma tran 2
            for j in range(len(A)): #hang ngang, ma tran 1
                for k in range(len(B)): #hang ngang ma tran 2
                    result[j][i] = result[j][i] + A[j][k]*B[k][i]         
        return result
    
    
    @staticmethod
    def MultiplyMatrix31(A=[], B=[]):
        #so cot cua A phai bang so Hang cua B
        '''
        if len(A[0]) != len(B):
            #print "matrix ", A, " \n multiply by matrix \n ", B, "\n is invalid" 
            return
        '''    

        result = [[0]*1 for i in range(3)]

        for i in range(1): #cot doc, ma tran 2
            for j in range(3): #hang ngang, ma tran 1
                for k in range(3): #hang ngang ma tran 2
                    result[j][i] +=  A[j][k]*B[k][i]         
        return result
    @staticmethod
    def MultiplyMatrix(A=[], B=[]):
        #so cot cua A phai bang so Hang cua B
        '''
        if len(A[0]) != len(B):
            #print "matrix ", A, " \n multiply by matrix \n ", B, "\n is invalid" 
            return
        '''    
        lenB = len(B[0])
        lenA = len(A)
        result = [[0]*lenB for i in range(lenA)]

        for i in range(lenB): #cot doc, ma tran 2
            for j in range(lenA): #hang ngang, ma tran 1
                for k in range(len(B)): #hang ngang ma tran 2
                    result[j][i] +=  A[j][k]*B[k][i]         
        return result
    @staticmethod
    def MatrixToPoint(matrix):
        result = (matrix[0][0],matrix[1][0])       
        return result
    
    @staticmethod
    def PointToMatrix(point=[]):
        result = [[0.0] for i in range(3)]
        result[0][0] = point[0]
        result[1][0] = point[1]
        result[2][0] = 1.0
        return result
    
    @staticmethod
    def InverseMatrix(A):
        '''
        1: tinh Determinant(matrix)
        2: tinh 9 he so
        3: chuyen vi ma tran 9 he so
        4: matran chuyen vi * 1/ det
        '''
        det = ((float)( A[0][0]*A[1][1]*A[2][2]+A[0][1]*A[1][2]*A[2][0]+A[0][2]*A[1][0]*A[2][1])-
               (A[2][0]*A[1][1]*A[0][2]+A[2][1]*A[1][2]*A[0][0]+A[2][2]*A[1][0]*A[0][1]))
        
        hs11 = A[1][1]*A[2][2]-A[2][1]*A[1][2]
        hs12 = -(A[1][0]*A[2][2]-A[2][0]*A[1][2])
        hs13 = (A[1][0]*A[2][1]-A[2][0]*A[1][1])
        hs21 = -(A[0][1]*A[2][2]-A[2][1]*A[0][2])
        hs22 = (A[0][0]*A[2][2]-A[2][0]*A[0][2])
        hs23 = -(A[0][0]*A[2][1]-A[2][0]*A[0][1])
        hs31 = (A[0][1]*A[1][2]-A[1][1]*A[0][2])
        hs32 = -(A[0][0]*A[1][2]-A[1][0]*A[0][2])
        hs33 = (A[0][0]*A[1][1]-A[1][0]*A[0][1])
        
        C = [[0]*3 for i in range(3)]
        C[0][0] = (float)(hs11)/det
        C[0][1] = (float)(hs21)/det
        C[0][2] = (float)(hs31)/det
        C[1][0] = (float)(hs12)/det
        C[1][1] = (float)(hs22)/det
        C[1][2] = (float)(hs32)/det
        C[2][0] = (float)(hs13)/det
        C[2][1] = (float)(hs23)/det
        C[2][2] = (float)(hs33)/det
        
        return C
        